package com.cl.edu.web.admin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.struts2.ServletActionContext;

import com.cl.common.util.UploadUtils;
import com.cl.core.web.struts2.Struts2Utils;
import com.opensymphony.xwork2.ActionSupport;

/**
 * fckeditor中所用图片上传
 * @author Administrator
 * 
 */
public class UploadAction extends ActionSupport
{

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 7178434481494830839L;

	private String				savePath			= File.separator+"upload"+File.separator+"news";

	private File				NewFile;
	private String				NewFileFileName;
	private String		 		NewFileContentType;

	@Override
	public String execute() throws Exception
	{

		// System.out.println("filename = "+getNewFileFileName());
		// 获取扩展名
		String ext = UploadUtils.getExtension(getNewFileFileName());
		// 重命名文件
		String newFileName = UploadUtils.generateFilename("", ext);

		// System.out.println(newFileName);
		int code = -1; // 返回结果代码
		String fullPath = getSavePath() + newFileName;
		String parentFolder = UploadUtils.getParentFolder(fullPath);
		try
		{

			File file = new File(parentFolder);
			UploadUtils.checkDirAndCreate(file);
			FileOutputStream fos = new FileOutputStream(fullPath);
			FileInputStream fis = new FileInputStream(getNewFile());
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0)
			{
				fos.write(buffer, 0, len);
			}
			code = 0;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			code = 1;
		}
		String imgPath = Struts2Utils.getRequest().getContextPath() + savePath + newFileName;

		String html = responseToString(code, imgPath.replace("\\", "/"));
		Struts2Utils.renderHtml(html);

		return null;
	}

	public String responseToString(Integer code, String fullPath)
	{
		StringBuffer sb = new StringBuffer(400);
		sb.append("<script type=\"text/javascript\">\n");
		// Compressed version of the document.domain automatic fix script.
		// The original script can be found at
		// [fckeditor_dir]/_dev/domain_fix_template.js
		sb.append("(function(){var d=document.domain;while (true){try{var A=window.parent.document.domain;break;}catch(e) {};d=d.replace(/.*?(?:\\.|$)/,'');if (d.length==0) break;try{document.domain=d;}catch (e){break;}}})();\n");
		sb.append("window.parent.OnUploadCompleted(").append(code).append(",'").append(fullPath).append("');");

		sb.append("</script>");

		return sb.toString();
	}

	public File getNewFile()
	{
		return NewFile;
	}

	public void setNewFile(File newFile)
	{
		NewFile = newFile;
	}

	public String getNewFileFileName()
	{
		return NewFileFileName;
	}

	public void setNewFileFileName(String newFileFileName)
	{
		NewFileFileName = newFileFileName;
	}

	public String getNewFileContentType()
	{
		return NewFileContentType;
	}

	public void setNewFileContentType(String newFileContentType)
	{
		NewFileContentType = newFileContentType;
	}

	public String getSavePath()
	{
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath)
	{
		this.savePath = savePath;
	}

}
